//
//  HistoryDispatchBillViewController.swift
//  ZNHXManagement
//
//  Created by 四川中农 on 2018/9/29.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit
import DZNEmptyDataSet
import MJRefresh

class HistoryDispatchBillViewController: BaseViewController {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var topFilterView: UIView!
    @IBOutlet weak var allBtn: UIButton!
    
    var dataArray = [InvoiceMode]()
    
    var viewLine:UIView!
    var lastSeleBtn:UIButton!
    
    var type = 0 // 0所有 1已完成 2未完成
    
    var pageNum = 1
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.title = "历史发货单"
        self.initViewLine()
        self.tableView.rowHeight = 60
        
        self.tableView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: { [unowned self] in
            self.pageNum = 1
            self.getInvoiceList()
        })
        
        self.tableView.mj_footer = MJRefreshAutoFooter.init(refreshingBlock: { [unowned self] in
            self.pageNum += 1
            self.getInvoiceList()
        })
        
        PKhud.show()
        getInvoiceList()
    }
    
    

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func initViewLine(){
        self.lastSeleBtn = self.allBtn
        self.viewLine = UIView.init(frame: CGRect.init(x: UIScreen.ScreenWidth / 3 / 2 - (38 / 2), y: 43.5, width: 38, height: 1.5))
        self.viewLine.backgroundColor = UIColor.init(hexColor: "#f7c201")
        self.topFilterView.addSubview(self.viewLine)
    }
    
    @IBAction func allBtnmethod(_ sender: UIButton) {
        self.checkSeleBtn(sender: sender)
    }
    
    @IBAction func pendingClick(_ sender: UIButton) {
        self.checkSeleBtn(sender: sender)
    }
    
    @IBAction func pendedClick(_ sender: UIButton) {
        self.checkSeleBtn(sender: sender)
    }
    
    func checkSeleBtn(sender:UIButton) {
        guard self.lastSeleBtn != sender else{
            return
        }
        self.pageNum = 1
        self.type = sender.tag - 100 - 1
        let width = UIScreen.ScreenWidth / 3
        UIView.animate(withDuration: 0.2) {
            self.viewLine.left = width * CGFloat(self.type + 1) - (width/2) - 38/2
        }
        
        sender.titleLabel?.font = UIFont.boldSystemFont(ofSize: 13)
        self.lastSeleBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        sender.isSelected = true
        self.lastSeleBtn.isSelected = false
        self.lastSeleBtn = sender
        
        getInvoiceList()
        
    }
    
}

extension HistoryDispatchBillViewController:UITableViewDataSource,UITableViewDelegate{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "HistoryDispatchBillTableViewCell") as! HistoryDispatchBillTableViewCell
        cell.model = self.dataArray[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let ctrl = Utils.instantiateInitialViewControllerWith(storyBoardName: "Main", identitier: "DispatchBillViewController") as! DispatchBillViewController
        ctrl.isHistory = true
        ctrl.id = self.dataArray[indexPath.row].id
        self.navigationController?.pushViewController(ctrl, animated: true)
    }
    
}


extension HistoryDispatchBillViewController:DZNEmptyDataSetDelegate,DZNEmptyDataSetSource{
    
    func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        let str = "空空如也~"
        let attribute = NSMutableAttributedString.init(string: str)
        attribute.addAttributes([NSAttributedStringKey.font:UIFont.systemFont(ofSize: 14),
                                 NSAttributedStringKey.foregroundColor:UIColor.init(hexColor: "666666")], range: NSRange.init(location: 0, length: str.count))
        
        return attribute
    }
    
    func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
        return UIImage.init(named: "img_Defaultheadimage")
    }
    
    func getInvoiceList(){
        HttpRequest.requestData(.post, URLString: Api.orderInvoiceList.rawValue, parameters: ["type":self.type,"pageNum":self.pageNum]) { (result) in
            PKhud.hiden()
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()
            if result["msg"] as! String == "success"{
                if let array = [InvoiceMode].deserialize(from: result["data"] as? [Any]) as? [InvoiceMode]{
                    if self.pageNum == 1 {
                        self.dataArray = array
                    }else{
                        self.dataArray += array
                    }
                    
                    if array.count < 10 {
                        self.tableView.mj_footer.endRefreshingWithNoMoreData()
                    }
                    self.tableView.reloadData()
                }
            }else{
                PKhud.showText(text: result["data"] as! String)
            }
        }
    }
}



















